package com.example.hehecat.yuejian1.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.RegisterContract;
import com.example.hehecat.yuejian1.mvp.presenter.RegisterPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity implements RegisterContract.View {

    @BindView(R.id.login_toolbar)
    Toolbar loginToolbar;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.btn_return)
    Button btnReturn;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password2)
    EditText etPassword2;
    private RegisterContract.Presenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        presenter = new RegisterPresenter(RegisterActivity.this);
    }

    @OnClick(R.id.btn_register)
    void btnRegisterOnClick() {
        if (regs()&&reg()) {
            presenter.register(etUsername.getText().toString(), etPassword.getText().toString(), etAddress.getText().toString(), etPhone.getText().toString());
        } else {
            return;
        }
    }

    @OnClick(R.id.btn_return)
    void btnReturnOnClick() {
        startActivity(new Intent(this, LoginActivity.class));
    }


    @Override
    public void moveToLoginView() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void showdata(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    private boolean regs() {
        String str1 = etUsername.getText().toString();
        String str2 = etPassword.getText().toString();


        if (str1.equals("") || str2.equals("")) {
            showdata("用户名或密码不能为空");
            return false;
        } else {
            return true;
        }
    }

    private boolean reg(){
        String str2 = etPassword.getText().toString();
        String str3 = etPassword2.getText().toString();

        if (!str2.equals(str3)){
            showdata("密码错误");
            return false;
        }else {
            return true;
        }
    }
}
